// 引入axios
import axios from "axios";
import METHOD from "./methods";
import Loading from "../../components/loading/index";

//请求方法
const BASEURL = "https://api.it120.cc/small4";

const axiosInstance = axios.create({
  baseURL: BASEURL,
  timeout: 100000,
});
let loading = null;
//请求拦截
axiosInstance.interceptors.request.use(
  function(config) {
    // 在发送请求之前做些什么
    //弹起login框
    setTimeout(() => {
      loading = Loading.serve();
      return config;
    }, 3000);
    
  },
  function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);
//响应拦截
axiosInstance.interceptors.response.use(
  function(response) {
    // 对响应数据做点什么
    loading.close()
    return response;
  },
  function(error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
/**
 *
 * @param {*} method
 * @param {*} url
 * @param {*} params
 */

export function request(method, url, params) {
  switch (method) {
    case METHOD.GET:
      return GET(url, params);
    case METHOD.POST:
      return POST(url, params);
  }
}

//请求方式
function GET(url, params) {
  return axiosInstance.get(url, params);
}

function POST(url, params) {
  return axiosInstance.post(url, params);
}
//上传文件和下载文件
// function UPLOAD(){

// }l

// function DOWNLOAD(){

// }
